#include "QQNavigationDelegate.h"

QQNavigationDelegate::QQNavigationDelegate(QObject* parent)
	: QStyledItemDelegate(parent)
{
	mItemSize = QSize(40, 40);
	mIconMargin = 3;
	mKeyPointsSize = QSize(8, 8);
}

QQNavigationDelegate::~QQNavigationDelegate()
{
}

void QQNavigationDelegate::setItemSize(QSize size)
{
	mItemSize = size;
}

QSize QQNavigationDelegate::getItemSize() const
{
	return mItemSize;
}

void QQNavigationDelegate::setPressIndex(const QModelIndex& index)
{
	mPressIndex = index;
}

QModelIndex QQNavigationDelegate::getPressIndex() const
{
	return mPressIndex;
}

void QQNavigationDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	QQNavigationNode* node = index.data(Qt::UserRole).value<QQNavigationNode*>();
	QPoint pos = option.rect.topLeft() +
		QPoint(option.rect.width() / 2 - mItemSize.width() / 2, option.rect.height() / 2 - mItemSize.height() / 2);
	QRect itemRect = QRect(pos, mItemSize);
	painter->save();
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
	QPainterPath path;
	path.addRoundedRect(itemRect, 5, 5);
	if (option.state & QStyle::State_Selected)
	{
		if (option.state & QStyle::State_MouseOver)
		{
			painter->fillPath(path, ElaThemeColor(ElaThemeType::ThemeMode::Light, BasicSelectedHoverAlpha));
		}
		else
		{
			painter->fillPath(path, ElaThemeColor(ElaThemeType::ThemeMode::Light, BasicSelectedAlpha));
		}
	}
	else
	{
		if (option.state & QStyle::State_MouseOver)
		{
			painter->fillPath(path, ElaThemeColor(ElaThemeType::ThemeMode::Light, BasicHoverAlpha));
		}
	}
	painter->restore();

	painter->save();
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);

	if (!node->getNodeIcon().isNull())
	{
		painter->save();
		painter->setPen(Qt::NoPen);
		QRect iconRect = itemRect.adjusted(mIconMargin, mIconMargin, -mIconMargin, -mIconMargin);
		QPixmap icon = node->getNodeIcon().pixmap(iconRect.width(), iconRect.height());
		painter->drawPixmap(iconRect, icon);
		painter->restore();
	}

	int keyPoints = node->getKeyPoints();
	if (keyPoints)
	{
		QPoint keyPos = itemRect.topRight() +
			QPoint(-mKeyPointsSize.width() / 2, mKeyPointsSize.height());
		painter->save();
		painter->setPen(Qt::NoPen);
		painter->setBrush(Qt::white);
		painter->drawEllipse(keyPos, mKeyPointsSize.width(), mKeyPointsSize.height());
		painter->setBrush(ElaThemeColor(ElaThemeType::ThemeMode::Light, StatusDanger));
		painter->drawEllipse(keyPos, mKeyPointsSize.width() - 1, mKeyPointsSize.height() - 1);

		painter->setPen(QPen(Qt::white, 2));
		QFont font = painter->font();
		font.setBold(true);
		if (keyPoints > 99)
		{
			keyPoints = 99;
		}
		if (keyPoints > 9)
		{
			font.setPixelSize(8);
		}
		else
		{
			font.setPixelSize(9);
		}
		painter->setFont(font);
		painter->drawText(keyPoints > 9 ? keyPos.x() - 5 : keyPos.x() - 3, keyPos.y() + 4, QString::number(keyPoints));
		painter->restore();
	}
	painter->restore();
}

QSize QQNavigationDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	return mItemSize;
}
